package com.lc.soft.service.impl;

import com.lc.soft.dao.IParameterDao;
import com.lc.soft.entity.CommonParameter;
import com.lc.soft.entity.ParameterEntity;
import com.lc.soft.service.IParameterService;
import com.lc.soft.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class ParameterService implements IParameterService {

    private IParameterDao parameterDao;

    @Autowired
    public ParameterService(IParameterDao parameterDao) {
        this.parameterDao = parameterDao;
    }

    @Override
    public ResponseResult create(ParameterEntity parameterEntity) {
        try {
            int createResult = parameterDao.create(parameterEntity);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(ParameterEntity parameterEntity) {
        try {
            int updateResult = parameterDao.update(parameterEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult updateState(ParameterEntity parameterEntity) {
        try {
            int updateResult = parameterDao.updateState(parameterEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findPagedParameterListByCondition(CommonParameter commonParameter) {
        try {
            List<ParameterEntity> parameterEntityList = parameterDao.findPagedParameterListByCondition(commonParameter);
            if (parameterEntityList == null || parameterEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            int amount = parameterDao.findAmountByCondition(commonParameter);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", parameterEntityList);
            resultMap.put("amount", amount);
            return ResponseResult.success(resultMap);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
